package com.leyou.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leyou.dto.BrandDTO;
import com.leyou.dto.PageDTO;
import com.leyou.pojo.Brand;
import com.leyou.pojo.CategoryBrand;
import com.leyou.service.BrandService;
import com.leyou.service.CategoryBrandService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jshstart
 * @creat 2020-12-09 22:14
 */
@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;
    @Autowired
    private CategoryBrandService categoryBrandService;
    /**
     * 根据品牌id查询品牌集合
     * @param ids
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam List<Long> ids){
        List<Brand> brandList = brandService.listByIds(ids);
        List<BrandDTO> dtoList = brandList.stream().map(BrandDTO::new).collect(Collectors.toList());
        return ResponseEntity.ok(dtoList);
    }

    /**
     * 根据品牌id查询品牌
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id") Long id){
        Brand brand = brandService.getById(id);
        return ResponseEntity.ok(new BrandDTO(brand));
    }
    @GetMapping("/page")
    public ResponseEntity<PageDTO<Brand>> queryBrandByPage(@RequestParam String key,@RequestParam Integer page,@RequestParam Integer rows){
        Page<Brand> brandPage = new Page<>();
        brandPage.setCurrent(page);
        brandPage.setSize(rows);
       PageDTO<Brand> pageDTO= brandService.queryBrandByPage(key,brandPage);
       return ResponseEntity.ok(pageDTO);
    }
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategoryId(@RequestParam Long id){
        List<CategoryBrand> categoryBrandList = categoryBrandService.query().eq("category_id", id).list();
        List<Long> brandIds = categoryBrandList.stream().map(categoryBrand -> categoryBrand.getBrandId()).collect(Collectors.toList());
        List<Brand> brands = brandService.listByIds(brandIds);
        List<BrandDTO> dtoList = brands.stream().map(BrandDTO::new).collect(Collectors.toList());
        return ResponseEntity.ok(dtoList);
//       List<Long> ids= categoryBrandService.queryBrandId(id);
//        List<Brand> brandList = brandService.listByIds(ids);
//        List<BrandDTO> dtoList = brandList.stream().map(BrandDTO::new).collect(Collectors.toList());
    }
   @PostMapping
    public ResponseEntity<Void> saveBrand( BrandDTO brandDTO){
        System.out.println("brandDTO = " + brandDTO);
       Brand brand = brandDTO.toEntity(Brand.class);
       brandService.saveOrUpdate(brand);
//       Brand brand1 = brandService.query().eq("name", brand.getName()).one();
//       categoryBrandService.add(brandDTO,brand1);
       List<Long> categoryIds = brandDTO.getCategoryIds();
       ArrayList<CategoryBrand> categoryBrands = new ArrayList<>();
       for (Long categoryId : categoryIds) {
           CategoryBrand categoryBrand = CategoryBrand.of(categoryId, brand.getId());
            categoryBrands.add(categoryBrand);
       }
       categoryBrandService.saveBatch(categoryBrands);
       return  ResponseEntity.status(201).build();
    }
   @PutMapping
    public ResponseEntity<Void> updateBrandById(BrandDTO brandDTO){
        System.out.println("brandDTO = " + brandDTO);
       Brand brand = brandDTO.toEntity(Brand.class);
       categoryBrandService.removeByBrandId(brand.getId());
       brandService.updateById(brand);
       categoryBrandService.add(brandDTO,brand);
       return  ResponseEntity.status(204).build();
    }
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteById(@PathVariable("id") Long id){
        brandService.deleteById(id);
        return ResponseEntity.status(204).build();
    }
//  AccessKey ID  LTAI4GD4v6gAJMy3Ls9jJaCu
//  AccessKey Secret ee6Rjv0NMb3J5dPetjgmlbCYrtr487
}
